﻿using System;
using System.Diagnostics;
using System.IO;

namespace Datagility.Shpt.SPDocMirror.Service
{
    /// <summary>
    /// Provides access to the folder on the file system to which a document should be written or from
    ///  which a document should be deleted as a result of changes to the document in a SharePoint Document 
    ///  Library.
    /// </summary>
    internal class ShadowFolder
    {
        private DirectoryInfo targetDir = null;

        /// <summary>
        /// Gets a reference to a file system folder.
        /// </summary>
        /// <param name="RootPath">The root document mirroring (or 'shadow') folder</param>
        /// <param name="DocFolder">The path to the required folder (relative to the root)</param>
        /// <param name="CreateIfNotFound">If true, the folder is created if it doesn't already exist</param>
        public ShadowFolder(string RootPath, string DocFolder)
        {
            SPMirrorLoggingService.Write("Information", "ShadowFolder.ctor Starting...");

            if (RootPath == null)
            {
                throw new ArgumentException("Parameter Cannot be Null", "RootPath");
            }

            DirectoryInfo dir = new DirectoryInfo(RootPath);
            if (!dir.Exists)
                dir.Create();

            if (DocFolder != null)
            {
                string targetDirPath = DocFolder.Replace(@"/", @"\");
                targetDirPath = dir.FullName + @"\" + targetDirPath;
                targetDir = new DirectoryInfo(targetDirPath);

                if (!targetDir.Exists)
                        targetDir.Create();
            }
            else
            {
                targetDir = dir;
            }

            SPMirrorLoggingService.Write("Information", String.Format("ShadowFolder.ctor Complete - targetDir = {0}", targetDir));
        }

        /// <summary>
        /// Writes a file to the referenced Shadow Folder
        /// </summary>
        /// <param name="FileName">The name of the file</param>
        /// <param name="DocFile">The file as a byte array</param>
        public void AddFile(string FileName, byte[] DocFile)
        {
            SPMirrorLoggingService.Write("Information", String.Format("ShadowFolder.AddFile Starting... - FileName = {0}", FileName));

            FileInfo fInfo = new FileInfo(targetDir.FullName + @"\" + FileName);

            FileStream fStrm = new FileStream(fInfo.FullName, FileMode.Create, FileAccess.ReadWrite);
            fStrm.Write(DocFile, 0, DocFile.Length);
            fStrm.Close();
            fStrm.Dispose();

            SPMirrorLoggingService.Write("Information", "ShadowFolder.AddFile Complete");
        }

        /// <summary>
        /// Deletes a file from the referenced Shadow Folder
        /// </summary>
        /// <param name="FileName">The name of the file</param>
        /// <param name="Strict">If true, an error is raised if the file to be deleted is not found</param>
        public void DeleteFile(string FileName, bool Strict)
        {
            SPMirrorLoggingService.Write("Information", String.Format("ShadowFolder.DeleteFile Starting... - FileName = {0}", FileName));

            FileInfo fInfo = new FileInfo(targetDir.FullName + @"\" + FileName);
            if (fInfo.Exists != true)
            {
                if (Strict == true)
                {
                    throw new FileNotFoundException(FileName);
                }
            }
            else
            {
                fInfo.Delete();
            }

            SPMirrorLoggingService.Write("Information", String.Format("ShadowFolder.DeleteFile Complete", FileName));
        }
    }
}
